﻿(function () {
    $(function () {
        var _controlMeasureService = abp.services.app.controlMeasure;
        var _$modal = $("#ControlMeasureCreateModal");
        var _$form = _$modal.find("form");

        var _$modaledit = $("#ControlMeasureEditModal");
        var _$formedit = _$modaledit.find("form");

        //新增保存
        _$form.find('button[type=submit]').click(function (e) {
            e.preventDefault();

            if (!_$form.valid()) {
                return;
            }
           //拿到HazardSourceId和RiskPointId及控制措施

            var HazardSourceId = $("#HazardSourceId").val();

            var RiskPointId = $("#RiskPointId").val();
            var Controlmeasures = new Array($("#controlmeasure1").val(), $("#controlmeasure2").val(), $("#controlmeasure3").val(), $("#controlmeasure4").val());
            //var hazardsource = _$form.serializeFormToObject();
            
            abp.ui.setBusy(_$modal);

            _controlMeasureService.insertControlMeasures(RiskPointId, HazardSourceId, Controlmeasures).done(function () {
                _$modal.modal('hide');
                location.reload(true); //reload page to see edited user!
            }).always(function () {
                abp.ui.clearBusy(_$modal);
            });
        });




        //打开新增窗口，传递参数HazardSourceId
        $('#ControlMeasureCreateModal').on('show.bs.modal', function (event) {
            var button = $(event.relatedTarget); // Button that triggered the modal
            var hazardsourceId = button.data('hazardsource-id'); // Extract info from data-* attributes
            var riskpointId = button.data('riskpoint-id');

            var modal = $(this);
            modal.find('#HazardSourceId').val(hazardsourceId);
            modal.find('#RiskPointId').val(riskpointId);

        });



        //刷新页面
        $("#RefreshButton").click(function () {
            refreshpost();
        });

        function refreshpost() {
            location.reload();
        }

        //删除

        $(".delete-controlmeasure").click(function () {
            var hazardsourceId = $(this).attr("data-controlmeasure-id");
            var hazardsourceName = $(this).attr("data-controlmeasure-name");
            
            deleteHazardSource(hazardsourceId, hazardsourceName);
        });

        function deleteHazardSource(hazardsourceId, hazardsourceName) {

            abp.message.confirm(
                abp.utils.formatString(abp.localization.localize('AreYouSureWantToDelete', 'DualControl'), hazardsourceName),
                function (isConfirmed) {
                    if (isConfirmed) {
                        _controlMeasureService.deleteControlMeasure({
                            id: hazardsourceId
                        }).done(function () {
                            refreshpost();
                        });
                    }
                }
            );
        }
      
        //打开编辑窗口，传递参数HazardSourceId
        $('#ControlMeasureEditModal').on('show.bs.modal', function (event) {
            var button = $(event.relatedTarget); // Button that triggered the modal
            var controlmeasureId = button.data('controlmeasure-id'); // Extract info from data-* attributes

            var controlcontent = button.data('controlmeasure-controlcontent');

            var modal = $(this);
            modal.find('#Id').val(controlmeasureId);
            
            modal.find("#ControlContent").val(controlcontent);
        })

        //编辑修改保存       

        _$formedit.find('button[type=submit]').click(function (e) {
            e.preventDefault();

            if (!_$form.valid()) {
                return;
            }
            var controlmeasure = _$formedit.serializeFormToObject();

            abp.ui.setBusy(_$modal);

            _controlMeasureService.createOrUpdateControlMeasure({ controlmeasure }).done(function () {
                _$modal.modal('hide');
                location.reload(true); //reload page to see edited user!
            }).always(function () {
                abp.ui.clearBusy(_$modal);
            });
        });


        //模态框关闭时，清除其中的内容。
        _$modal.on("hide.bs.modal", function () {
            _$form[0].reset();
        });
    });
})();